package com.neusoft.elm.controller;

import com.neusoft.elm.po.Cart;
import com.neusoft.elm.service.CartService;
import com.neusoft.elm.service.Impl.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 购物车
 * Controller层
 */
public class CartController {

    /**
     * 新增购物车
     *
     * @param request
     * @return
     * @throws Exception
     */
    public Object saveCart(HttpServletRequest request) throws Exception{
        Cart cart = new Cart();
        //从前端获取数据
        cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
        cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
        cart.setUserId(request.getParameter("userId"));
        //进入Service层
        CartService service = new CartServiceImpl();
        int result = service.saveCart(cart);
        //向前端返回数据
        return result;
    }

    /**
     * 更新购物车
     *
     * @param request
     * @return
     * @throws Exception
     */
    public Object updateCart(HttpServletRequest request) throws Exception{
        Cart cart = new Cart();
        //从前端获取数据
        cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
        cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
        cart.setUserId(request.getParameter("userId"));
        cart.setQuantity(Integer.valueOf(request.getParameter("quantity")));
        //进入Service层
        CartService service = new CartServiceImpl();
        int result = service.updateCart(cart);
        //向前端返回数据
        return result;
    }

    /**
     * 删除购物车
     *
     * @param request
     * @return
     * @throws Exception
     */
    public Object removeCart(HttpServletRequest request) throws Exception{
        Cart cart = new Cart();
        //从前端获取数据
        cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
        cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
        cart.setUserId(request.getParameter("userId"));
        //进入Service层
        CartService service = new CartServiceImpl();
        int result = service.removeCart(cart);
        //向前端返回数据
        return result;
    }

    public Object listCart(HttpServletRequest request) throws Exception{
        Cart cart = new Cart();
        cart.setUserId(request.getParameter("userId"));
        if(request.getParameter("business")!=null){
            cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
        }
        //进入Service层
        CartService service = new CartServiceImpl();
        List<Cart> list = service.listCart(cart);
        //向前端返回数据
        return list;
    }
}
